const mongoose = require('mongoose')
// 连接mongodb数据库
mongoose.connect('mongodb://127.0.0.1:27017/bilibili')

mongoose.connection.once('open', async () => { 
    console.log('连接成功！')

    const bookSchema = new mongoose.Schema({
        name:String,
        author:String,
        price:Number,
        is_hot:Boolean
    })
    let bookModel = mongoose.model('novel',bookSchema)

    // bookModel.find({author:'余华'}).then((res) => { 
    //     console.log('获取成功！',res)
    //  }).catch((error) => { 
    //     console.log('获取失败：',error)
    //   })


    // bookModel.findById('66cc2969a73ebfe85143e815').then((res) => { 
    //     console.log('获取成功！',res)
    //  }).catch((error) => { 
    //     console.log('获取失败：',error)
    //   })

    let result = await bookModel.find()
    console.log('------------------------------------')
    console.log(result)



 })
mongoose.connection.on('close',() => { 
    console.log('关闭连接！')
 })